package com.xujf.sym.ui.base

import android.content.pm.ActivityInfo
import android.os.Build
import android.os.Bundle
import android.support.annotation.Nullable
import net.opacapp.multilinecollapsingtoolbar.CollapsingToolbarLayout
import android.support.design.widget.FloatingActionButton
import android.support.v7.widget.Toolbar
import android.view.View
import android.view.WindowManager
import android.widget.ImageView
import com.xujf.sym.R
import kotlinx.android.synthetic.main.activity_base_collapse.*

/**
 * Created by XuJiafeng on 2018/2/5.
 * Content：标准滑动压缩Activity——collapsing_toolbar_layout
 */
abstract class BaseCollapseActivity : BaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            //浅色状态栏-黑色字体（Android 6.0以上）
            window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
//            window.statusBarColor = resources.getColor(R.color.compatible_translucent_status)
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            window.statusBarColor = resources.getColor(R.color.compatible_translucent_status)
        }
        setUpUI(savedInstanceState)
    }

    abstract fun setUpUI(savedInstanceState: Bundle?)

    override fun setContentView(layoutResID: Int) {
        if (requestedOrientation != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
            requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
        }
        super.setContentView(R.layout.activity_base_collapse)
        initTitleView()
        View.inflate(this, layoutResID, fl_content)
    }

    private fun initTitleView() {
        toolbar.title = title
        setSupportActionBar(toolbar)
        supportActionBar!!.setDisplayHomeAsUpEnabled(true)
        supportActionBar!!.setDisplayShowHomeEnabled(true)

//        var top = 0
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//            top += ResUtil.getStatusBarHeight(this)
//        }
//        (toolbar.layoutParams as FrameLayout.LayoutParams).topMargin = top

        toolbar.setNavigationOnClickListener(object : PerfectClickListener() {
            override fun onNoDoubleClick(v: View?) {
                finish()
            }
        })
        collapsing_toolbar_layout.title = getString(R.string.app_name)
        //压缩后的title颜色
        collapsing_toolbar_layout.setCollapsedTitleTextColor(resources.getColor(R.color.title_bar_text))
//        collapsing_toolbar_layout.setExpandedTitleColor(resources.getColor(R.color.orange))
    }

    fun setRightText(text: String?, @Nullable icon: Int?, btnClick: View.OnClickListener) {
        rl_tv_right.visibility = View.VISIBLE
        if (text != null) {
            tv_right.text = text
        }
        if (icon != null) {
            tv_right.setBackgroundResource(icon.toInt())
//            val linearParams = btn_right.layoutParams
//            linearParams.height = StatusUtil.dip2px(this, 40f)
//            linearParams.width = StatusUtil.dip2px(this, 40f)
//            btn_right.layoutParams = linearParams
        }
        rl_tv_right.setOnClickListener(btnClick)
    }

    fun setRightImg(icon: Int?, btnClick: View.OnClickListener) {
        rl_iv_right.visibility = View.VISIBLE
        if (icon != null) {
            iv_right.setBackgroundResource(icon.toInt())
        }
        rl_iv_right.setOnClickListener(btnClick)
    }

    fun getCollapsingToolbarLayout(): CollapsingToolbarLayout {
        return collapsing_toolbar_layout
    }

    fun getBaseToolBar(): Toolbar {
        return toolbar
    }

    fun getTopImageView(): ImageView {
        return iv_big_img
    }

    fun getFAButton(): FloatingActionButton {
        return fab
    }
}
